package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;

import java.util.List;

import flc.ast.AppConfig;
import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.HotSearchAdapter;
import flc.ast.databinding.ActivitySearchBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

public class SearchActivity extends BaseAc<ActivitySearchBinding> {

    private String key;
    private HotSearchAdapter hotSearchAdapter;

    @Override
    protected int onCreate() {
        return R.layout.activity_search;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivSearchBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvSearch.setOnClickListener(this);

        FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(this);
        layoutManager.setFlexDirection(FlexDirection.ROW);
        layoutManager.setJustifyContent(JustifyContent.FLEX_START);
        mDataBinding.rvHotSearchList.setLayoutManager(layoutManager);
        hotSearchAdapter = new HotSearchAdapter();
        mDataBinding.rvHotSearchList.setAdapter(hotSearchAdapter);
        hotSearchAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        key = hotSearchAdapter.getItem(position).getName();
        mDataBinding.etSearch.setText(key);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvSearch://搜索
                key = mDataBinding.etSearch.getText().toString().trim();
                if (key.isEmpty()) {
                    ToastUtils.showShort(getString(R.string.please_input_text));
                    return;
                }
                SearchResultActivity.key = key;
                startActivity(SearchResultActivity.class);
                break;
        }
    }

    @Override
    protected void initData() {
        getData();
    }

    private void getData() {
        StkResApi.getTagResourceList(this, AppConfig.HOT_SEARCH_DATA, StkResApi.createParamMap(0, 8), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        hotSearchAdapter.setList(result);
                    }
                }
            }
        });
    }

}